package com.weavernorth.humanresource.formal.dao;

import com.weavernorth.humanresource.formal.vo.FormalUser;
import com.weavernorth.util.Logger4jUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: ecology_erdos
 * @description: 员工转正流程触发接口实现
 * @author: zm
 * @create: 2019-12-16 16:54
 **/


public class FormalUserDaoImpl implements FormalUserDao {
//    private static Log newlog = LogFactory.getLog(FormalUserDaoImpl.class);
private Logger newlog = Logger4jUtil.getLoggerByName("/Timmer/humanresource/formal/formaluser/");
    /**
     * 查询所有试用员工信息
     *
     * @return List<FormalUser>
     */
    @Override
    public List<FormalUser> getOnTryUsers() {
        BaseBean bb=new BaseBean();

        //数据库操作
        RecordSet rsSelect = new RecordSet();
        //返回用户信息
        List<FormalUser> formalUsersList = new ArrayList<FormalUser>();
        //当前日期
        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        // new Date()为获取当前系统时间
        String strCurrentTime = (df.format(new Date()));
        //获取转正多少天来触发定时任务
        String strDays=Util.null2String(bb.getPropValue("humanMess","formaldays"));
        FormalUser formalUser;
        //查询试用员工信息
        String userSql = "select uz.lcsscj,uz.rybh,hr.jobtitle,hr.id,uz.zzrq,hr.workcode,cf.field9,hr.sex,hr.birthday,cf.field0,cf.field2,cf.field11,cf.field14,cf.field15," +
                "cf.field56,cf.field31,cf.field33,hr.departmentid,cf.field3,cf.field4,cf.field5,cf.field6,cf.field7,cf.field23," +
                "cf.field47,cf.field36,cf.field37,cf.field38,cf.field39,cf.field40,cf.field41,cf.field42 " +
                " from uf_zzcftz uz,hrmresource hr,cus_fielddata cf where uz.rybh=hr.workcode and hr.id=cf.id and uz.ryzt=0 and cf.scopeid=1 and  uz.zzrq=(select to_char(sysdate + interval '"+strDays+"' day, 'yyyy-mm-dd') from dual)";
        newlog.info("查询试用用户信息sql:" + userSql);
        rsSelect.executeQuery(userSql);
        while (rsSelect.next()) {

            formalUser = new FormalUser();
            //流程id
            formalUser.setStrLcid(Util.null2String(rsSelect.getString("lcsscj")));
            //人员id
            formalUser.setStrUserid(Util.null2String(rsSelect.getString("id")));
            //员工编码
            formalUser.setStrWorkcode(Util.null2String(rsSelect.getString("workcode")));
            //证件号
            formalUser.setStrCreNo(Util.null2String(rsSelect.getString("field9")));
            //性别
            formalUser.setStrSex(Util.null2String(rsSelect.getString("sex")));
            //出生日期
            formalUser.setStrBirthDay(Util.null2String(rsSelect.getString("birthday")));
            //民族
            formalUser.setStrNation(Util.null2String(rsSelect.getString("field0")));
            //国籍
            formalUser.setStrNationality(Util.null2String(rsSelect.getString("field2")));
            //户口所在地
            formalUser.setStrHukouAddress(Util.null2String(rsSelect.getString("field11")));
            //户口类型
            formalUser.setStrHukouType(Util.null2String(rsSelect.getString("field12")));
            //政治面貌
            formalUser.setStrPolitic(Util.null2String(rsSelect.getString("field14")));
            //最高学历
            formalUser.setStrDegree(Util.null2String(rsSelect.getString("field15")));
            //试用期
            formalUser.setStrTyial(Util.null2String(rsSelect.getString("field56")));
            //入职日期
            formalUser.setStrEntryDate(Util.null2String(rsSelect.getString("field31")));
            //转正日期
            formalUser.setStrCurrectDate(Util.null2String(rsSelect.getString("zzrq")));
            //部门名称
            formalUser.setStrDepartment(Util.null2String(rsSelect.getString("departmentid")));
            //根据部门id在标准矩阵表里查找部门负责人
            String strChargeId = getChargeid(Util.null2String(rsSelect.getString("departmentid")));
            //部门负责人
            formalUser.setStrDepartmentCharge(Util.null2String(strChargeId));

            //岗位名称
            formalUser.setStrJob(Util.null2String(rsSelect.getString("jobtitle")));
            //员工组
            formalUser.setStrStaff(Util.null2String(rsSelect.getString("field4")));
            //员工子组
            formalUser.setStrSubgroup(Util.null2String(rsSelect.getString("field5")));
            //人事范围
            formalUser.setStrPersonnelScope(Util.null2String(rsSelect.getString("field6")));
            //人事子范围
            formalUser.setStrPersonnelSubScope(Util.null2String(rsSelect.getString("field7")));
            //工资核算范围
            formalUser.setStrSalaryScope(Util.null2String(rsSelect.getString("field23")));
            //辅导员
            formalUser.setStrInstructor(Util.null2String(rsSelect.getString("field47")));
            //薪资类型
            formalUser.setStrSalayScope(Util.null2String(rsSelect.getString("field36")));
            //薪资区域
            formalUser.setStrSalaryArea(Util.null2String(rsSelect.getString("field37")));
            //薪资岗级别
            formalUser.setStrSalaryPositionLevel(Util.null2String(rsSelect.getString("field38")));
            //薪资岗档次
            formalUser.setStrSalaryPostGrade(Util.null2String(rsSelect.getString("field39")));
            //基本工资
            formalUser.setStrBasePay(Util.null2String(rsSelect.getString("field40")));
            //绩效工资
            formalUser.setStrMeritPay(Util.null2String(rsSelect.getString("field41")));
            //年终奖金基数
            formalUser.setStrYearEndBase(Util.null2String(rsSelect.getString("field42")));
            //放入list
            formalUsersList.add(formalUser);


        }

        return formalUsersList;
    }


    /**
     * 根据部门id查找部门矩阵的部门负责人
     */

    private String getChargeid(String strDepartid) {
        //返回部门负责人id
        String strResult = "";
        //数据库操作
        RecordSet rs = new RecordSet();
        String sql = "select bmfzr from matrixtable_2 where id=?";
        newlog.info("查找部门负责人:" + sql + ",部门id:" + strDepartid);
        rs.executeQuery(sql, strDepartid);
        if (rs.next()) {
            strResult = Util.null2String(rs.getString("bmfzr"));
        }
        return strResult;

    }


    /**
     * 判断转正日期的前一个月是否是当前日期
     */

    private String getFlag(String strCurrentDate, String strFormalDate) {
        //基础类
        BaseBean bb = new BaseBean();
        //返回状态
        String strFlag = "false";
        //获取前一个月的年
        String strPreYear = "";
        //获取前一月的月份
        String strPreMonth = "";
        //转正的天
        String strDay = "";
        //解析转正日期
        if (!"".equals(strFormalDate)) {
            String strMonth = strFormalDate.split("-")[1];
            strDay = strFormalDate.split("-")[2];
            //当月份为1时,则前一个月,年份减1,月份为12月
            if (!"".equals(strMonth) && Integer.parseInt(strMonth) == 1) {
                String strYear = strFormalDate.split("-")[0];
                if (!"".equals(strYear)) {
                    strPreYear = String.valueOf(Integer.parseInt(strYear) - 1);
                    strPreMonth = "12";
                }
            } else if (!"".equals(strMonth)) {
                strPreMonth = String.valueOf(Integer.parseInt(strMonth) - 1);
            }
        }
        //得到转正前一个月的日期
        String strPreDate = strPreYear + "-" + strPreMonth + "-" + strDay;
        newlog.info("转正前一个月的日期:" + strPreDate);
        //与当前日期对比
        if (strCurrentDate.equals(strPreDate)) {
            strFlag = "true";
        }
        return strFlag;
    }

//    /**
//     * 根据requestid得到workflowid
//     *
//     * @param strRequestid
//     * @return
//     */
//    public String getWorkflowid(String strRequestid) {
//        RecordSet rs = new RecordSet();
//        //返回值
//        String strWorkflowid = "";
//        rs.executeQuery("select workflowid from workflow_requestbase where requestid=?", strRequestid);
//        if (rs.next()) {
//            strWorkflowid = Util.null2String(rs.getString("workflowid"));
//        }
//        return strWorkflowid;
//    }

    /**
     * 由workcode得到人员id
     * @param strWorkcode
     * @return
     */

    public String getUserid(String strWorkcode) {
        RecordSet rs = new RecordSet();
        //返回人员id
        String strUserid = "";
        rs.executeQuery("select id from hrmresource where workcode=? and (accounttype is null or accounttype=0)");
        if (rs.next()) {
            strUserid = Util.null2String(rs.getString("id"));
        }
        return strUserid;
    }
}
